package com.tool_stdy.Spider;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取电影网站的 种子地址
 * @author yan_li
 *
 */
public class NetSpider2 {

	private static List<String> list = new ArrayList<String>();
	private static volatile List<String> urlList = new ArrayList<String>();

	public static void getUrls(String beginUrl) throws Exception {
		URL url = new URL(beginUrl);
		URLConnection openConnection = url.openConnection();
		InputStream inputStream = openConnection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				inputStream));
		String urlRegex = "http://www.loldytt.com/Dongzuodianying/\\w+/";
		String line = "";
		Pattern p_Url = Pattern.compile(urlRegex);
		int count = 0;
		while ((line = reader.readLine()) != null) {
			Matcher url_ma = p_Url.matcher(line);
			while (url_ma.find()) {
				urlList.add(url_ma.group());
				count++;
			}
			if (count >= 100) {
				break;
			}

		}
	}

	public static void getContent(String str_url) throws Exception {
		URL url = new URL(str_url);
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		if(urlConn.getResponseCode()!=200){
			return;
		}
		InputStream inputStream = urlConn.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				inputStream,"gbk"));

		String regexEmails = "<li><a href=\"thunder:.+</a></li>";
		String line = "";
		Pattern p_email = Pattern.compile(regexEmails);
		while ((line = reader.readLine()) != null) {
			Matcher ma = p_email.matcher(line);
			while (ma.find()) {
				list.add(ma.group());
			}
		}

	}

	public static void main(String[] args) throws Exception {
		String urlBegin = "http://www.loldytt.com/";
		getUrls(urlBegin);
		for (String url : urlList) {
			getContent(url);
			System.out.println("==========" + url);
		}
		System.out.println("***********************************");
		for (String mail : list) {
			System.out.println(mail);
		}
//		 String src="<span id=\"s1p10\"><a href= \"thunder://QUFmdHA6Ly93d3c6cGlhb2h1YS5jb21AZHkxMjYucGlhb2h1YS5jb206MzAzNzUvxq67qLXn07BwaWFvaHVhLmNvbcvZtsjT67ykx+k3QkQxMjgwuN/H5bzTs6Sw5tbQ06LLq9fWLnJtdmJaWg== \" title= \"速度与激情7BD1280高清加长版中英双字 \" target= \"_self \">速度与激情7BD1280高清加长版中英双字</a></span>";
//		 String urlRegex="<span id=.*\"\\w+.*\"><a href=.+</span>";
//		 System.out.println(src.matches(urlRegex));
	}
}
